package cs2340.quarterblack.wheresmystuff;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import android.widget.Spinner;

public class Item implements Serializable{
	
	public enum Itemtype {LOST, FOUND};
	public enum Category {CLOTHING,PERSONAL,FURNATURE,OTHER};
	private static final long serialVersionUID = 1L;
	private static String defaultName = "";
	private static String defaultDescription = "";
	private static Date defaultDate = new Date(1,1,1);
	private static String defaultLocation = "";
	private static Category defaultCategory = Category.OTHER;
	private String name;
	private String description;
	private String location;
	private Date date;
	private Category category;
	private String userId;
	private Itemtype type;
	public static List<String> categoryList;// = {"CLOTHING","PERSONAL","FURNATURE","OTHER"};
	
	
	/**
	 * sets the main 2 paramaters of an item
	 * @param account the account this item belongs to
	 * @param itemName the name of the item
	 */
	public Item(String userId, String itemName)
	{
		this.userId = userId;
		name = itemName;
		description = defaultDescription;
		location = defaultLocation;
		date = defaultDate;
		category = defaultCategory;
	}
	/**
	 * this constructor sets all of the fields for the item class
	 * @param userId the User Id of the person who owns this item
	 * @param name the name of the item
	 * @param description a brief description of the item
	 * @param location the user input location that the item was either lost or found in
	 * @param date the date the item was either lost or found
	 * @param category the category the item falls under
	 */
	public Item(String userId,String name,String description,String location,Date date,Category category)
	{
		this.userId = userId;
		this.name = name;
		this.description = description;
		this.location = location;
		this.date = date;
		this.category = category;
	}
	/**
	 * returns the name and part of the description of the item
	 */
	public String toString()
	{
		if(Model.getItemRep() == 0)
		{
			if(description != "")
			{
				if(description.length() < 30)
				{
					return name + "\n" + "description: " + description;
				}
				else
				{
					return name + "\n" + "description: " + description.substring(0, 29);
				}
			}
			else
			{
				return name;
			}
		}
		else if(Model.getItemRep() == 1)
		{
			return toStringCategory();
		}
		else
		{
			if(date != null)
			{
				return name + "\n" + "Date: " + date.toString();
			}
			else
			{
				return name + "\n" + "Date: " + "No date for this item";
			}
		}
	}
	/**
	 * returns the name and part of the description of the item
	 */
	public String toStringCategory()
	{
		if(category != null)
		{
			return name + "\n" + "Category: " + getStringCategory();
		}
		else
		{
			return name;
		}
	}
	/**
	 * adds the item to the found list of it's account
	 * @return whether the item was successfully added or not
	 */
	public boolean addItem()
	{
		Storage storage = StorageManager.getInstance();
		return storage.addItem(this);
	}

	/**
	 * gets the item name
	 * @return the name of the item
	 */
	public String getName() {
		return name;
	}
	/**
	 * Sets the name of the item
	 * @return item name
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * gets the description of the item
	 * @return item description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * sets the description of the item
	 * @param description the description of the item
	 */
	public void setDescription(String description) {
		this.description = description;
	}
	
	/**
	 * gets the location of the item
	 * @return location the location of the item
	 */
	public String getLocation() {
		return location;
	}
	/**
	 * sets the location of the item
	 * @return location item location 
	 */
	public void setLocation(String location) {
		this.location = location;
	}
	/**
	 * gets the date the item was either found or lost
	 * @return date the item date
	 */
	public Date getDate() {
		return date;
	}

	/**
	 * sets the date the item was either found or lost
	 * @param date the date the item is to be given
	 */
	public void setDate(Date date) {
		this.date = date;
	}

	/**
	 * gets the category of the item
	 * @return category the category the item is in
	 */
	public Category getCategory() {
		return category;
	}

	/**
	 * sets the category of the item
	 * @param category the category the item is to be set to 
	 */
	public void setCategory(Category category) {
		this.category = category;
	}

	/**
	 * gets the Id of the user this item belongs to
	 * @return userId the string:userId
	 */
	public String getUserId() {
		return userId;
	}

	/**
	 * sets the userId
	 * @param userId the string the userId is to be set to
	 */
	public void setUserId(String userId) {
		this.userId = userId;
	}

	/**
	 * returns whether the item is lost or found
	 * @return type the type that the item is categorized as
	 */
	public String getType() {
		switch (type) {
		case LOST:
			return "lost";
		case FOUND:
			return "found";
		default:
			return "unknown";
		}
	}

	/**
	 * sets the type of the item, whether it is lost or found
	 * @param type the type that the item is categorized as
	 */
	public void setType(Itemtype type) {
		this.type = type;
	}
	
	public String getContact()
	{
		if(userId != null)
		{
			Storage storage = StorageManager.getInstance();
			Account account = storage.getAccount(new Account(userId,"dkdk"));
			if(account != null)
			{
				return account.getEmail();
			}
		}
		return null;
	}
	/**
	 * gets a list of the available categories for an item to be a part of
	 * @return the variable categoryList;
	 */
	public static List<String> getCategoryList()
	{
		categoryList = new LinkedList<String>();
		categoryList.add("CLOTHING");
		categoryList.add("PERSONAL");
		categoryList.add("FURNATURE");
		categoryList.add("OTHER");
		return categoryList;
	}
	/**
	 * given the position of the category selector, returns the category associated with it
	 * @param pos the position of the category selector
	 * @return a category
	 */	
	public static Category getNumberedCategory(int pos)
	{
		Category catCategory = null;
			if(pos == 0)
			{
				catCategory = Item.Category.CLOTHING;
			}
			else if(pos == 1)
			{
				catCategory = Item.Category.PERSONAL;
			}
			else if(pos == 2)
			{
				catCategory = Item.Category.FURNATURE;
			}
			else if(pos == 3)
			{
				catCategory = Item.Category.OTHER;
			}
			return catCategory;
	}
	/**
	 * returns the category as a string
	 * @return the category
	 */
	public String getStringCategory()
	{
		if(category == Category.CLOTHING)
		{
			return "Clothing";
		}
		else if(category == Category.FURNATURE)
		{
			return "Furniture";
		}
		else if(category == Category.OTHER)
		{
			return "Other";
		}
		else if(category == Category.PERSONAL)
		{
			return "Personal";
		}
		return "No category";
	}
}